//
//  TKIMSrvDtwMsgView.m
//  TKIM
//
//  Created by liupm on 15/11/20.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMSrvDtwMsgView.h"
//#import "TKIMSrvRichMessage.h"
//#import "TKIMDateHelper.h"

@interface TKIMSrvDtwMsgView()
//图文消息标题
@property(nonatomic,strong) UILabel *titleLabel;
//图文消息时间
@property(nonatomic,strong) UILabel *timeLabel;
//图片视图
@property(nonatomic,strong) UIImageView *picView;

//内容摘要描述
@property(nonatomic,strong) UILabel *contentDescription;
//底部线条
@property(nonatomic,strong) UILabel *line;
//底部视图
@property(nonatomic,strong) UILabel *bottomView;

@property(nonatomic,strong) TKIMSrvRichMessage *tmplMsg;

@end

@implementation TKIMSrvDtwMsgView

/**
 *  @Author lpm, 15-07-30 11:07:12
 *
 *  @brief  组装界面
 */
-(void)drawView
{
    [self addSubview:self.backgroundImageView];
    
    self.backgroundImageView.clipsToBounds = YES;
//    [self.backgroundImageView addSubview:self.timeLabel];
    [self.backgroundImageView addSubview:self.picView];
    [self.backgroundImageView addSubview:self.titleLabel];

    [self.backgroundImageView addSubview:self.contentDescription];
    [self.backgroundImageView addSubview:self.line];
    [self.backgroundImageView addSubview:self.bottomView];
    
    self.backgroundImageView.layer.cornerRadius = 5.0f;
    self.backgroundImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.backgroundImageView.layer.borderWidth = 0.5f;
    self.backgroundImageView.backgroundColor= [UIColor whiteColor];
    
    UITapGestureRecognizer *toDetail = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toDetail:)];
    self.backgroundImageView.userInteractionEnabled = YES;
    [self.backgroundImageView addGestureRecognizer:toDetail];
    
}

-(void)toDetail:(UITapGestureRecognizer *)tap
{
    [self routerEventWithName:TKIMMsgClickEvent userInfo:_tmplMsg];
    [self routerEventWithName:TKIMSrvTemplOnclick userInfo:_tmplMsg.items.lastObject];
    
    _tmplMsg.state = TKIMMsgSendStateRead;
    [[TKIMDBManager shareInstance] updateMsg:_tmplMsg];
    
    if (_tmplMsg.state == TKIMMsgSendStateRead) {
        self.titleLabel.textColor = [TKUIHelper colorWithHexString:@"#9A9A9A"];
        self.bottomView.textColor = [TKUIHelper colorWithHexString:@"#9A9A9A"];
    }else{
        self.titleLabel.textColor = [TKUIHelper colorWithHexString:@"#333333"];
        self.bottomView.textColor = [TKUIHelper colorWithHexString:@"#333333"];
    }
}


-(void)layoutSubviews{
    [super layoutSubviews];
//    _contentDescription.frame =CGRectMake(self.timeLabel.left,self.picView.bottom,TKIMScreenWidth*0.9-20,60);
    _contentDescription.width=TKIMScreenWidth*0.95-20;

}



-(void)setMessage:(TKIMMessage *)message
{
    [super setMessage:message];
    _tmplMsg = (TKIMSrvRichMessage *)message;
    TKIMSrvRichItem *item = (TKIMSrvRichItem *)_tmplMsg.items.lastObject;
    self.titleLabel.text = item.title;
    
    NSDate *createDate = [TKIMDateHelper stringToDate:message.time];
    if (createDate) {
        self.timeLabel.text = [TKIMDateHelper formatDate:createDate withFormat:@"YYYY年MM月dd日"];
    }else{
        self.timeLabel.text = [TKIMDateHelper formatDate:[NSDate new] withFormat:@"YYYY年MM月dd日"];
    }
    
//    self.timeLabel.text = [TKIMDateHelper formatDate:[NSDate new] withFormat:@"MM月dd日"];
//     self.bottomView.text=message.msgRemark;
    self.bottomView.text = @"查看详情";
    self.contentDescription.text = item.contentDescription;
    [self.picView sd_setImageWithURL:[NSURL URLWithString:item.picUrl]];
    CGSize size = [self.contentDescription sizeThatFits:CGSizeMake(self.contentDescription.width, CGFLOAT_MAX)];
    self.contentDescription.frame = CGRectMake(self.contentDescription.left, self.contentDescription.top, TKIMScreenWidth*0.95-20, size.height+15);
//    self.backgroundImageView.frame = CGRectMake(TKIMScreenWidth*0.05, 0, TKIMScreenWidth*0.9, self.titleLabel.bottom+self.timeLabel.height+self.contentDescription.height+self.line.height+self.bottomView.height+self.picView.height);
    
    self.backgroundImageView.frame = CGRectMake(TKIMScreenWidth*0.025, 10, TKIMScreenWidth*0.95, self.picView.bottom + self.titleLabel.height + self.contentDescription.height + self.line.height + self.bottomView.height);
    
//    self.frame =  CGRectMake(self.left, self.top, self.backgroundImageView.width, self.backgroundImageView.height+TKIMChatMsgPadding);
    
    self.frame =  CGRectMake(self.left, self.top, self.backgroundImageView.width, self.backgroundImageView.height);
    
    message.size = self.frame.size;
    self.line.frame =   CGRectMake(self.titleLabel.left, self.backgroundImageView.height-40, self.titleLabel.width, 1.0f);
    self.bottomView.frame =   CGRectMake(self.titleLabel.left, self.backgroundImageView.height-40, self.titleLabel.width, 40);
    
    if (message.state == TKIMMsgSendStateRead) {
        self.titleLabel.textColor = [TKUIHelper colorWithHexString:@"#9A9A9A"];
        self.bottomView.textColor = [TKUIHelper colorWithHexString:@"#9A9A9A"];
    }else{
        self.titleLabel.textColor = [TKUIHelper colorWithHexString:@"#333333"];
        self.bottomView.textColor = [TKUIHelper colorWithHexString:@"#333333"];
    }
    
}


/**
 *  @Author lpm, 15-11-05 09:11:23
 *
 *  @brief  行高
 *
 *  @return
 */
+(CGFloat)heightForRow:(TKIMMessage *)msg
{
    TKIMSrvRichMessage *m = (TKIMSrvRichMessage *)msg;
    TKIMSrvRichItem *item = (TKIMSrvRichItem *)m.items.lastObject;
    CGSize size = [TKIMStringHelper sizeForFont:[UIFont systemFontOfSize:14.0] size:CGSizeMake(TKIMScreenWidth*0.8, CGFLOAT_MAX) mode:NSLineBreakByCharWrapping text:item.contentDescription];
    return 281+size.height+TKIMChatMsgPadding;
}



-(UILabel *)titleLabel
{
    if (!_titleLabel)
    {
//        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,10,TKIMScreenWidth*0.9-20,40)];
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,self.picView.bottom,TKIMScreenWidth*0.95-20,40)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.numberOfLines = 2;
        _titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    }
    return _titleLabel;
}

-(UILabel *)timeLabel
{
    if (!_timeLabel)
    {
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.left,self.titleLabel.bottom,self.titleLabel.width,30)];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.font = [UIFont systemFontOfSize:13.0f];
        _timeLabel.textColor = [UIColor grayColor];
        _timeLabel.numberOfLines = 2;
        _timeLabel.lineBreakMode = NSLineBreakByCharWrapping;

    }
    return _timeLabel;
}

-(UIImageView *)picView
{
    if (!_picView)
    {
//        _picView = [[UIImageView alloc] initWithFrame:CGRectMake(self.timeLabel.left, self.timeLabel.bottom, self.timeLabel.width, 160)];
        _picView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, TKIMScreenWidth*0.95, 160)];
        _picView.backgroundColor = [TKIMColorHelper stringToColor:@"#cccccc"];
    }
    return _picView;
}

-(UILabel *)contentDescription
{
    if (!_contentDescription)
    {
//        _contentDescription = [[UILabel alloc] initWithFrame:CGRectMake(self.timeLabel.left,self.picView.bottom,TKIMScreenWidth*0.9-20,60)];
        _contentDescription = [[UILabel alloc] initWithFrame:CGRectMake(self.timeLabel.left,self.titleLabel.bottom,TKIMScreenWidth*0.95-20,60)];
        _contentDescription.backgroundColor = [UIColor clearColor];
        _contentDescription.font = [UIFont systemFontOfSize:14.0f];
        _contentDescription.textColor = [UIColor grayColor];
        _contentDescription.numberOfLines = 0;
        _contentDescription.lineBreakMode = NSLineBreakByCharWrapping;
        
    }
    return _contentDescription;
}


-(UILabel *)line
{
    if (!_line)
    {
        _line = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.left, self.backgroundImageView.height-40, self.titleLabel.width, 1.0f)];
        _line.backgroundColor =[TKIMColorHelper stringToColor:@"#f4f4f4"];
    }
    return _line;
}


-(UILabel *)bottomView
{
    if (!_bottomView)
    {
        _bottomView = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.left, self.backgroundImageView.height-40, self.titleLabel.width, 40)];
        _bottomView.font = [UIFont systemFontOfSize:14.0f];
        _bottomView.text = @"阅读全文";
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(_bottomView.width-20, 10, 20, 20)];
        img.image = TKIMGetImage(@"im_icon_arrow_right");
        [_bottomView addSubview:img];

    }
    return _bottomView;
}
@end
